import BaseView from "../../../cfw/mvc/BaseView";
import GlobalEvent from "../../../cfw/event/GlobalEvent";
import { EventName, decorationList } from "../../../public/Config";
import DecorationManager from "../DecorationManager";
import ModuleManager, { ModuleID } from "../../../cfw/module/ModuleManager";
import ResItem from "../../../cfw/res/ResItem";
import { ResType } from "../../../cfw/res/ResInterface";
const { ccclass, property } = cc._decorator;

@ccclass
export default class DecorationView extends BaseView {
    @property([cc.Sprite])
    clothes: cc.Sprite[] = []
    @property([cc.Sprite])
    backs: cc.Sprite[] = []
    protected initPos: cc.Vec2[] = []
    protected model: DecorationManager;
    protected types: number[];

    start() {
        this.model = DecorationManager.instance();
        let clotheList = this.model.getClotheList()
        // cc.log('this.clothes.length ', this.clothes.length)

        for (let index = 0; index < this.clothes.length; index++) {
            let idx = this.types[index] - 1
            const element = clotheList[idx];
            this.updateSelectItem(idx, element)

        }
        // cc.log('this.initPos.length ', this.initPos.length)
        GlobalEvent.instance().on(EventName.SELECT_DECORATION_ITEM, this.updateSelectItem, this)
    }

    setInitPos() {
        if (this.initPos.length == 0) {
            for (let index = 0; index < this.clothes.length; index++) {
                this.initPos[index] = cc.v2()
                this.initPos[index].x = this.clothes[index].node.x;
                this.initPos[index].y = this.clothes[index].node.y;
            }
        }

    }

    onDestroy() {
        GlobalEvent.instance().off(EventName.SELECT_DECORATION_ITEM, this.updateSelectItem, this)
    }


    updateSprite(sprite: cc.Sprite, path: string) {
        ModuleManager.getLoader(ModuleID.DECORATION).loadRes(path, ResType.SpriteFrame, (err, res: ResItem) => {
            if (err || !cc.isValid(this.node)) {
                return;
            }
            sprite.spriteFrame = res.getRes()
            // cc.log(' this.clothes[index] scale ', this.clothes[index].node.scale)
        })
    }
    updateSelectItem(idx, clotheID) {
        // let type: number = clothe.getClotheType()
        // let index = type - 1;
        this.setInitPos()
        let index = decorationList[idx]
        cc.log('updateSelectItem index ', index, ' idx ', idx, this.initPos.length)
        this.clothes[index].node.x = this.initPos[index].x;
        this.clothes[index].node.y = this.initPos[index].y;

        if (clotheID > 0) {
            let clothe = this.model.getClothe(clotheID)
            let offset = clothe.getOffset()
            if (offset && offset.length > 0) {
                this.clothes[index].node.x += offset[0]
                this.clothes[index].node.y += offset[1]
            }
            this.updateSprite(this.clothes[index], clothe.getSprite())
            // ModuleManager.getLoader(ModuleID.DECORATION).loadRes(clothe.getSprite(), ResType.SpriteFrame, (err, res: ResItem) => {
            //     if (err || !cc.isValid(this.node)) {
            //         return;
            //     }
            //     this.clothes[index].spriteFrame = res.getRes()
            //     // cc.log(' this.clothes[index] scale ', this.clothes[index].node.scale)
            // })

            let back = this.backs[index]
            if (back) {
                let backImg = clothe.getBack();
                if (backImg) {
                    // ModuleManager.getLoader(ModuleID.DECORATION).loadRes(backImg, ResType.SpriteFrame, (err, res: ResItem) => {
                    //     if (err || !cc.isValid(this.node)) {
                    //         return;
                    //     }
                    //     back.spriteFrame = res.getRes()
                    //     // cc.log(' this.clothes[index] scale ', this.clothes[index].node.scale)
                    // })
                    this.updateSprite(back, backImg)
                } else {
                    back.spriteFrame = null;
                }
            }

        } else {
            if (this.model.canDel(idx)) {
                cc.log(' this.model.canDel(index) ', this.model.canDel(idx), ' index ', index)
                this.clothes[index].spriteFrame = null;
                if (this.backs[index]) {
                    this.backs[index].spriteFrame = null;
                }
            }

        }

    }

}
